<template>
<!--任务处理-->
<Card style="height:800px">
  <header>
    <div class="header_div">
      <div class="item" @click="waitDecision" style="cursor:pointer;color:#639DF9">
        待同步
        <span>{{decision}}</span> 份
      </div>
      <div class="item">
        待处理
        <span>{{waiting}}</span> 份
      </div>
      <div class="item">
        处理中
        <span>{{processing}}</span> 份
      </div>
      <div class="item">
        退回补件
        <span>{{patch}}</span> 份
      </div>
      <!--<div class="item" @click="handleProcessed">
        已处理
        <span>{{processed}}</span> 份
      </div>-->
      <div class="item" @click="addItem" style="cursor:pointer;color:#639DF9">
        已处理
        <span>{{processed}}</span> 份
      </div>
      <div class="div_bt">
        <Button type="primary" size="small" @click="handleDecsionData"  style="margin-left: 20px;">同步</Button>
        <Button type="primary" size="small" @click="handleProcessForMe" style="margin-left: 20px;">取件</Button>
      </div>
      <div style="clear: both;"></div>
    </div>
  </header>
  <!-- 等待跑决策 -->
  <Content style="margin-top:20px;" v-if="isShowPop==1">
    <Table border :columns="waitDecisionCols" :data="waitDecisionTableData"></Table>
  </Content>
  <div style="text-align:center;width:50%;margin:100px auto;" v-if="isShowPop==1">
    <Page :total="totalWD" :current="currentPageWD" :page-size="pageSizeWD" show-elevator show-total @on-change="handlePageChangeWD"></Page>
  </div>
  <!-- 处理中 -->
  <Content style="margin-top:20px;" v-if="isShowPop==2">
    <Table border :columns="case_table_columns" :data="case_table_datas"></Table>
  </Content>
  <div style="text-align:center;width:50%;margin:100px auto;" v-if="isShowPop==2">
    <Page :total="total" :current="currentPage" :page-size="pageSize" show-elevator show-total @on-change="handlePageChange"></Page>
  </div>
</Card>
</template>

<script>
export default {
  data () {
    return {
      isShowPop: 2,
      decision: 0,
      waiting: 1,
      processing: 0, // 处理中
      patch: 0, // 退回补件
      processed: 0, // 已处理
      isShowTab: 1,
      waitDecisionCols: [{
        type: 'index'
      },
      {
        title: '名称',
        key: 'custName'
      },
      {
        title: '身份证',
        key: 'custCardNo'
      },
      {
        title: '申请编号',
        key: 'loanId'
      },
      {
        title: '当前节点',
        key: 'curNodeName'
      },
      {
        title: '申请时间',
        key: 'createTime'
      }
      ],
      waitDecisionTableData: [],
      totalWD: 0,
      currentPageWD: 0,
      pageSizeWD: 20,
      case_table_columns: [{
        title: '客户姓名',
        key: 'custName',
        render: (h, params) => {
          const vm = this
          return h('div', {
            on: {
              click () {
                vm.$router.push({
                  name: 'task_detail',
                  query: {
                    loanId: params.row.loanId,
                    type: 1,
                    collapseCotrol: false
                  }
                })
              }
            },
            style: {
              'color': '#20a0ff',
              'cursor': 'pointer'
            }
          }, params.row.custName)
        }
      },
      {
        title: '身份证',
        key: 'custCardNo'
      },
      {
        title: '当前环节',
        key: 'curNodeName'
      },
      {
        title: '进件号',
        key: 'loanId'
      },
      {
        title: '挂起时间',
        key: 'hangTime'
      },
      {
        title: '产品',
        key: 'productTypeName'
      },
      {
        title: '业务类型',
        key: 'ywlx'
      }
      ],
      case_table_datas: [],
      total: 100,
      currentPage: 1,
      pageSize: 20
    }
  },
  methods: {
    handleDecsionData () {
      var params = {}
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/loan/service/runDecision', params).then(re => {
        if (re.data.code === 'success') {
          this.$Message.success({
            title: '提示',
            content: '本次处理' + re.data.data + '笔贷款!',
            onOk () {}
          })
        } else {
          this.$Message.warning({
            title: '提示',
            content: '内容：' + re.data.message,
            onOk () {}
          })
        }
      }).catch(er => {

      })
    },
    waitDecision (goPage) {
      this.isShowTab = 2
      var length = this.waitDecisionTableData.length
      // 没有加载过则加载数据
      if (length === 0) {
        var params = {}
        if (parseFloat(goPage).toString() === 'NaN') {
          goPage = 1
        }
        params.pageNum = goPage
        params.status = 'J00'
        this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/loan/service/wdList', params).then(re => {
          // console.log(re.data);
          if (re.data.code === 'success') {
            this.totalWD = re.data.data.total
            this.pageSizeWD = re.data.data.pageSize
            this.currentPageWD = re.data.pageNum
            this.waitDecisionTableData = []
            var datas = re.data.data.list || []
            datas.forEach(item => {
              var tData = {}
              tData.name = item.custName
              tData.icNum = item.custCardNo
              tData.loanId = item.loanId
              if (item.applyTime != null) {
                tData.createTime = this.$util.formatTime(new Date(item.applyTime))
              }
              this.waitDecisionTableData.push(tData)
            })
          } else {
            this.$Message.warning({
              title: '提示',
              content: '22' + re.data.data,
              onOk () {}
            })
          }
        }).catch(er => {

        })
      }
    },
    addItem () {
      this.$router.push({
        name: 'taskProcessed',
        query: {
          type: 0
        }
      })
    },
    handleProcessed () {
      this.isShowTab = 1
    },
    handleTabClick (name) {
      this.activeTabName = name
    },
    handlePageChangeWD (index) {
      waitDecision(index)
    },
    handlePageChange (index) {
      this.currentPage = index
      this.getMyProcesssData(index)
    },
    // 查询自己名下的件
    getMyProcesssData (goPage) {
      var params = {}
      if (parseFloat(goPage).toString() === 'NaN') {
        goPage = 1
      }
      params.pageNum = goPage
      // console.info(this.$appContext.marvels_core_approve + 'approve/loanapplycase/service/selflist')
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/loanapplycase/service/selflist/' + goPage).then(re => {
        // console.log(re.data.data);
        if (re.data.code === 'success') {
          this.total = re.data.data.total
          this.pageSize = re.data.data.pageSize
          this.currentPage = re.data.data.pageNum
          this.case_table_datas = []
          var datas = re.data.data.list || []
          var list = this.$util.cloneObject(datas)
          list.forEach(item => {
            if (item.hangTime) {
              item.hangTime = this.$util.formatTime(new Date(item.hangTime))
            }
            if (item.status === 0) {
              item.status = '处理中'
            } else if (item.status === 2) {
              item.status = '正常结案'
            } else if (item.status === 3) {
              item.status = '异常结案'
            } else if (item.status === 9) {
              item.status = '挂起'
            } else if (item.status === 1) {
              item.status = '草稿'
            }
            if (item.caseType === 0) {
              item.ywlx = '新进件'
            } else {
              item.ywlx = '补件'
            }
            this.case_table_datas = list
          })
        } else {
          this.$Message.warning({
            title: '提示',
            content: '333' + re.data.data,
            onOk () {}
          })
        }
      }).catch(er => {

      })
    },
    // 取件
    handleProcessForMe () {
      var params = {
        nodeCode: sessionStorage.nodeCode,
        groupCode: sessionStorage.groupCode
      }
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/process/service/handle_process', params).then(re => {
        // console.log(re.data);
        if (re.data.code === 'success') {
          this.query_status_num()
          this.getMyProcesssData(1)
          this.$Message.success({
            title: '提示',
            content: re.data.data.custName + '被取到!',
            onOk () {}
          })
        } else {
          this.$Message.warning({
            title: '提示',
            content: re.data.data,
            onOk () {}
          })
        }
      }).catch(er => {

      })
    },
    // 查询取件数量
    query_status_num () {
      var params = {}
      // api.getProcessStatusNumData().then(re=>{
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/process/service/query_status_num', params).then(re => {
        if (re.data.code === 'success') {
          this.decision = re.data.data.decision
          this.waiting = re.data.data.waiting
          this.processing = re.data.data.processing
          this.patch = re.data.data.patch
          this.processed = re.data.data.processed
        } else {
          this.$Message.warning({
            title: '提示',
            content: re.data.message,
            onOk () {}
          })
        }
      }).catch(er => {

      })
    },
    refresh () {
      this.query_status_num()
      this.getMyProcesssData(1)
    }
  },
  created () {
    this.$ajax = this.$util.ajax()
    this.query_status_num()
    this.getMyProcesssData(1)

    setInterval(() => { // 自动刷新
      this.refresh()
    }, 300000)
  }

}
</script>

<style lang="less">
.header_div {
  width: 100%;
  padding: 0px 0px 0px 100px;
}

.header_div .item {
  margin: 20px 5px 0px 10px;
  width: 130px;
  float: left;
}

.header_div .div_bt {
  margin: 20px 100px 0px 10px;
  float: right;
}
</style>
